//
//  ExitLoginService.m
//  OMS
//
//  Created by goldeneye on 14/10/19.
//  Copyright (c) 2014年 ShangHai Goldeneye Techonology Co.,Ltd. All rights reserved.
//

#import "ExitLoginService.h"
#import "UserLoginNSCoding.h"
#import "LocalNotification.h"
#import "NSUserDefaultsUserLogin.h"
#import "JGProgressHUD.h"
#import "UIAlertView+EnumView.h"
#import "QueryMessageService.h"
#import "PlaySoundUtil.h"
#import "GetUserStationService.h"
#import "BasicConfigurationService.h"

#import "UserLoginViewController.h"
#import "FirstUserVerificationController.h"

#import "CLLockVC.h"
#import "FingerprintLock.h"
#import "JPushHttpService.h"
#import "MessageInfoService.h"

@interface ExitLoginService()<UIAlertViewDelegate>

@end

@implementation ExitLoginService


#pragma mark -
#pragma mark UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    [ExitLoginService exitAppUserLogin];
}

#pragma mark -
#pragma mark exitLogin

+ (void)removePasswdLock
{
    //移除手势密码和指纹
    [CLLockVC colsePwd];
    [FingerprintLock removePasscode];
}

+ (void) exitAppUserLogin
{
    JGProgressHUD *HUD = [self showSimpleHUDText:@"正在退出登录中..."];
    [self clearUserLoginData];
    [HUD dismiss];
}

+ (void) clearUserLoginData
{
    MainAppDelegate *application = (MainAppDelegate*)[[UIApplication sharedApplication] delegate];
    [UIAlertView dismissAll];
    
    //销毁音效
    [PlaySoundUtil disposeAllSound];
    
    //清理站点信息
    [[GetUserStationService sharedService] clearStationData];
    
    //清理基础配置信息
    [[BasicConfigurationService sharedService] clearBasicConfigurationData];
    
    //关闭消息获取
    [[QueryMessageService sharedService] stopQueryMessage];
    
    //关闭基本信息获取，删除数据
    [[GetUserBasicInfoService sharedService] stopMenuAuthorityService];
    
    //删除所有通知消息
    [LocalNotification cancelAllLocalNotifications];
    
    //删除用户登录信息
    [NSUserDefaultsUserLogin exitLoginDeleteSaveData];
    
    //删除所有消息
    [MessageInfoService deleteALLEntity];
    
    //更新APP图标标记
    [LocalNotification setAPPIconBadgeNumber:0];
    [LocalNotification cancelAllLocalNotifications];
    
    //取消注册设备RegistrationID
    [JPushHttpService deviceStateRegisterChange];
    
    //清除网页cookies及缓存
    NSHTTPCookie *cookie;
    NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    for (cookie in [storage cookies]) [storage deleteCookie:cookie];
    [[NSURLCache sharedURLCache] removeAllCachedResponses];

    //删除缓存文件
    NSString *cacheDir=[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    [[NSFileManager defaultManager]removeItemAtPath:cacheDir error:nil];
    
    //关闭其他页面，跳转到初始设置页面
    UIViewController *viewController = [application.navigationController.viewControllers lastObject];
    ConfigurationCodeing *model = [ConfigurationHelper getConfigurationData];
    if (!model) {
        NSMutableArray *newControllers = [NSMutableArray array];
        FirstUserVerificationController *userLogin=[[FirstUserVerificationController alloc] init];
        [newControllers addObject:userLogin];
        [application.navigationController setViewControllers:newControllers animated:YES];
        [application.navigationController setToolbarHidden:YES animated:YES];
    }
    
    //关闭相关页面,跳转到登录页面
    else if (![viewController isKindOfClass:[UserLoginViewController class]]) {
        NSMutableArray *newControllers = [NSMutableArray array];
        UserLoginViewController *userLogin=[[UserLoginViewController alloc] init];
        [newControllers addObject:userLogin];
        [application.navigationController setViewControllers:newControllers animated:YES];
        [application.navigationController setToolbarHidden:YES animated:YES];
    }
    
    //以上未执行处理(其他)
    else {
        [application.navigationController popToRootViewControllerAnimated:YES];
    }
    
    //关闭压顶页面
    [application.navigationController dismissViewControllerAnimated:YES completion:nil];
}


#pragma mark -
#pragma mark JGProgressHUD

+ (JGProgressHUD *)showSimpleHUDText:(NSString*)text {
    
    JGProgressHUD *HUD = [JGProgressHUD progressHUDWithStyle:JGProgressHUDStyleDark];
    HUD.textLabel.text = @"加载中...";
    
    if(text && text.length>0)
        HUD.textLabel.text = text;
    
    JGProgressHUDFadeZoomAnimation *an = [JGProgressHUDFadeZoomAnimation animation];
    HUD.animation = an;
    
    HUD.backgroundColor = [UIColor colorWithWhite:0.0f alpha:0.4f];
    HUD.HUDView.layer.shadowColor = [UIColor blackColor].CGColor;
    HUD.HUDView.layer.shadowOffset = CGSizeZero;
    HUD.HUDView.layer.shadowOpacity = 0.4f;
    HUD.HUDView.layer.shadowRadius = 8.0f;
    
    MainAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
    [HUD showInView:delegate.navigationController.navigationController.view];
    return HUD;
}

@end
